# -*- coding: utf-8 -*-
"""
@Time ：2021/3/23 20:14 
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：Web自动化特斯汀学院电商项目关键字库
"""
import re
from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.select import Select

from common.Logger import path
from common.ver.slide import Slide
from common.ver.verify import Verify


class Web:
    """关键字类"""

    def __init__(self):
        # 代表当前运行用例的浏览器类型
        self.br = 'gc'
        # 运行自动化的驱动对象
        self.driver: webdriver.Chrome = None
        # 关联的字典
        self.relations = {}
        # 验证码识别
        self.verify = Verify('wuqingfqng', '6e8ebd2e301f3d5331e1e230ff3f3ca5', '96001')

    def __find_ele(self, locator: str = ''):
        """
        统一定位的关键字
        :param locator: 元素定位器，同时支持xpath，id
        :return: 定位到的元素，如果没有定位到就返回None
        """
        if locator is None or locator == '':
            return None
        elif locator.startswith('/'):
            # 如果是斜杆开头，我们使用xpath定位
            return self.driver.find_element_by_xpath(locator)
        elif locator.startswith('#') or locator.__contains__('>'):
            # 如果是#开头或者包含>，那么就用css定位
            return self.driver.find_element_by_css_selector(locator)
        else:
            # 默认使用id定位
            return self.driver.find_element_by_id(locator)

    def __get_relations(self, params: str = ''):
        """
        获取关联
        :param params: 需要关联的参数
        :return: 返回关联后的字符串
        """
        if params:
            for key in self.relations:
                params = params.replace('{' + key + '}', str(self.relations.get(key)))

        return params

    def openbrowser(self, browser: str = 'gc'):
        """
        打开浏览器
        :param browser: 选择需要打开的浏览器类型，gc：谷歌浏览器(默认)，ff：Firefox浏览器，ie：IE浏览器
        :return:
        """
        if browser == 'gc' or browser == '':
            self.br = 'gc'
            self.driver = webdriver.Chrome()
        elif browser == 'ff':
            self.br = 'ff'
            self.driver = webdriver.Firefox()
        elif browser == 'ie':
            self.br == 'ie'
            self.driver = webdriver.Ie()
        else:
            self.br = 'gc'
            print('浏览器类型不支持，默认使用Chrome')
            self.driver = webdriver.Chrome()

        # 隐式等待
        self.driver.maximize_window()
        self.driver.implicitly_wait(4)

    def geturl(self, url: str = ''):
        """
        访问网站
        :param url: 需要打开的网站地址
        :return:
        """
        self.sleep('1')
        self.driver.get(url)

    def gettitle(self):
        """
        获取网站当前title
        :return:
        """
        title = self.driver.title
        self.relations['title'] = title

    def click(self, locator=None):
        """
        点击元素
        :param locator:元素定位器，同时支持xpath，css，id
        :return:
        """
        locator = self.__get_relations(locator)
        ele = self.__find_ele(locator)
        ele.click()

    def clickjs(self, locator: str = ''):
        """
        通过js点击元素
        :param locator:元素定位器，同时支持xpath，css，id
        :return:
        """
        locator = self.__get_relations(locator)
        ele = self.__find_ele(locator)
        self.driver.execute_script('arguments[0].click();', ele)

    def input(self, locator: str = '', value: str = ''):
        """
        文本输入
        :param locator:元素定位器，同时支持xpath，css，id
        :param value: 需要输入的文本
        :return:
        """
        value = self.__get_relations(value)
        ele = self.__find_ele(locator)
        if value.endswith('.png'):
            value = path + '/lib/imgs/' + value
        ele.send_keys(value)

    def intoiframe(self, locator: str = ''):
        """
        进入iframe
        :param locator:元素定位器，同时支持xpath，css，id
        :return:
        """
        ele = self.__find_ele(locator)
        self.driver.switch_to.frame(ele)

    def outiframe(self):
        """退出iframe"""
        self.driver.switch_to.default_content()

    def select(self, locator: str = '', value: str = ''):
        """
        下拉框选择
        :param locator:元素定位器，同时支持xpath，css，id
        :param value:选择的值
        :return:
        """
        ele = self.__find_ele(locator)
        select = Select(ele)
        try:
            int(value)
            select.select_by_value(value)
        except:
            select.select_by_visible_text(value)

    def moveto(self, locator: str = ''):
        """
        把鼠标移动到元素上面
        :param locator:元素定位器，同时支持xpath，css，id
        :return:
        """
        ele = self.__find_ele(locator)
        action = ActionChains(self.driver)
        # perform代表使这个action操作生效
        action.move_to_element(ele).perform()

    def gettext(self, locator: str = '', reg: str = ''):
        """
        获取文本并保存为text全局变量
        :param locator:元素定位器，同时支持xpath，css，id
        :param reg: 取数据的正则表达式
        :return:
        """
        ele = self.__find_ele(locator)
        text = ele.text
        if reg:
            text = re.findall(reg, text)[0]

        print(text)
        self.relations['text'] = text

    def saveparam(self, paramname, value):
        """
        增加一个参数
        :param paramname: 参数名
        :param value: 参数值
        :return:
        """
        value = self.__get_relations(value)
        self.relations[paramname] = value

    def switchwin(self, idx: str = ''):
        """关闭旧窗口，切换到新窗口"""
        handles = self.driver.window_handles
        print(handles)
        if idx:
            # 切换到新窗口
            self.driver.switch_to.window(handles[int(idx)])
        else:
            # 关闭旧窗口，只能关闭当前操作的窗口
            self.driver.close()
            # 切换到新窗口
            self.driver.switch_to.window(handles[1])

    def getverify(self, locator: str = ''):
        """
        获取图片验证码的文字
        :param locator: 元素定位器，同时支持xpath，css，id
        :return:
        """
        ele = self.__find_ele(locator)
        # 先获取到验证码图片
        ele.screenshot(path + '/lib/verify/verify.png')
        # 调验证码的库识别验证码
        ver = self.verify.PostPic(1004, im=path + '/lib/verify/verify.png')
        self.relations['verify'] = ver

    def slide(self, locator: str = '', locator1: str = '', locator2: str = ''):
        # 滑块验证码对象
        slide_obj = Slide(self.driver)
        slide_obj.slide_img(locator, locator1, locator2)

    def sleep(self, t: str = '0.5'):
        """
        固定等待
        :param t: 需要等待的时间ms
        :return:
        """
        t = float(t)
        sleep(t)

    def quit(self):
        """退出浏览器"""
        self.driver.quit()
